package com.bdqn.controller;


import com.bdqn.entity.AddressBook;
import com.bdqn.entity.ShoppingCart;
import com.bdqn.entity.User;
import com.bdqn.param.requset.AddressParam;
import com.bdqn.service.AddressBookService;
import com.bdqn.service.ShoppingCartService;
import com.bdqn.service.UserService;
import com.bdqn.util.Constants;
import com.bdqn.util.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-06-11
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService bookService;
    @Autowired
    private UserService userService;
    /**
     * 通过user_id获取到绑定user的所有地址信息
     *
     * @return
     */
    @GetMapping("/list")
    public RespBean list(AddressBook addressBook, HttpServletRequest request) {
        //通过用户登录时存的手机号进行获取用户信息   手机号(唯一) == user_id
        String phone = (String) request.getSession().getAttribute(Constants.LOGIN_SESSION);
        //通过用户id 查询其所有的地址信息
        List<AddressBook> list = bookService.selectByUserId(phone);
        return list != null ? RespBean.ok("获取地址信息成功", list) : RespBean.error("获取地址信息失败");
    }

    /**
     * 设置默认的收货地址
     * @return
     */
    @PutMapping("/default")
    public RespBean setDefaultAddress(@RequestBody AddressParam addressParam) {
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(addressParam.getIsDefault()).setId(addressParam.getId());
        //创建一个新的集合来存储对象
        List<AddressBook> addressBooks = new ArrayList<>();
        //循环遍历所有的对象的isDefault为0
        for (AddressBook book : bookService.list()) {
            addressBooks.add(book.setIsDefault(0));
        }
        //同时修改多个对象 -->集合
        bookService.updateBatchById(addressBooks);
        //添加新的默认收获地址的时候 先将之前的默认收货地址状态设为0
        boolean flag = bookService.updateById(addressBook);
        return flag ? RespBean.ok("修改默认收货地址成功", flag) : RespBean.error("修改默认收货地址失败");
    }

    /**
     * 通过id进行收货地址的信息回显
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public RespBean toUpdate(@PathVariable Long id) {
        AddressBook byId = bookService.getById(id);
        return byId != null ? RespBean.ok("通过收获地址id进行回显地址信息成功", byId) : RespBean.error("通过收获地址id进行回显地址信息失败");
    }

    /**
     * 通过接收前台传来的修改信息 进行对地址信息的修改
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public RespBean update(@RequestBody AddressBook addressBook) {
        boolean flag = bookService.updateById(addressBook);
        return flag ? RespBean.ok("修改地址信息成功", flag) : RespBean.error("修改地址信息失败");
    }

    /**
     * 获取前台传递的参数信息  进行添加地址信息的操作
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public RespBean add(@RequestBody AddressBook addressBook, HttpSession session) {
        //通过获取手机号进行获取用户id
        String phone = (String) session.getAttribute(Constants.LOGIN_SESSION);
        // 流程: 手机号-->user-->user_id-->addressBook赋值
        User user = userService.selectByPhone(phone);
        boolean save = bookService.save(addressBook.setUserId(user.getId()).setCreateUser(user.getId()).setUpdateUser(user.getId()));
        return save ? RespBean.ok("添加地址信息成功", save) : RespBean.error("添加地址信息失败");
    }

    /**
     * 获取默认的地址
     * @return
     */
    @GetMapping("/default")
    public RespBean defaultAddress(HttpSession session){
        Constants.CREAT_ORDER_TIME=System.currentTimeMillis();//当作创建订单的时间
        List<AddressBook> addressBooks = bookService.selectByUserId(session.getAttribute(Constants.LOGIN_SESSION).toString());
        AddressBook addressBook=null;
        for (AddressBook addressBook1 : addressBooks) {
            if(addressBook1.getIsDefault()==1){
                addressBook=addressBook1;
            }
        }
        return addressBook!=null?RespBean.ok("获取默认地址成功",addressBook):RespBean.error("获取默认地址失败");
    }
}
